//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "FormSendTesterReport.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "DBCtrlsEh"
#pragma link "FIBDatabase"
#pragma link "FIBQuery"
#pragma link "pFIBDatabase"
#pragma link "pFIBQuery"
#pragma link "PropFilerEh"
#pragma link "PropStorageEh"
#pragma resource "*.dfm"
TSendTesterReportForm *SendTesterReportForm;
//---------------------------------------------------------------------------

__fastcall TSendTesterReportForm::TSendTesterReportForm(TComponent* Owner)
	: TForm(Owner)
{
	PropStorageEh->Section = this->Name;
	Constraints->MinWidth = Width;
	Constraints->MinHeight = Height;
	EditReportID = -1;
}
//---------------------------------------------------------------------------

void __fastcall TSendTesterReportForm::FormShow(TObject *Sender)
{
	if(EditReportID >= 0) {
		ReportDetailsDBEditEh->Text = EditReportText;
		ReportCategoryRadioGroup->Enabled = false;
		ReportCategoryRadioGroup->ItemIndex = -1;
		ReportDetailsDBEditEhChange(ReportDetailsDBEditEh);
	}
	else {
		ReportDetailsDBEditEh->Clear();
		ReportCategoryRadioGroup->ItemIndex = -1;
		ReportDetailsDBEditEhChange(ReportDetailsDBEditEh);
	}
	EditReportTextOnStart = ReportDetailsDBEditEh->Text;
}
//---------------------------------------------------------------------------

void __fastcall TSendTesterReportForm::ReportDetailsDBEditEhChange(TObject *Sender)
{
	StatusBar->Panels->Items[0]->Text = String(ReportDetailsDBEditEh->MaxLength - ReportDetailsDBEditEh->Text.Length()) + " characters left";
}
//---------------------------------------------------------------------------

void __fastcall TSendTesterReportForm::SendButtonClick(TObject *Sender)
{
	if(EditReportID >= 0) {
		UpdateQuery->ParamByName("ID")->AsInteger = EditReportID;
		UpdateQuery->ParamByName("TEXT")->AsString = ReportDetailsDBEditEh->Text;
		UpdateQuery->ExecQuery();
		EditReportID = -1;
		Close();
	}
	else {
		if(ReportCategoryRadioGroup->ItemIndex >= 0) {
			InsertQuery->ParamByName("CONTROL_NAME")->AsString = ReportedControlLabel->Caption;
			InsertQuery->ParamByName("CATEGORY_ID")->AsInteger = ReportCategoryRadioGroup->ItemIndex;
			InsertQuery->ParamByName("TEXT")->AsString = ReportDetailsDBEditEh->Text;
			InsertQuery->ExecQuery();
			EditReportID = -1;
			Close();
		}
		else MessageDlg("Select Category", mtError, TMsgDlgButtons()<<mbOK, NULL);
	}
}
//---------------------------------------------------------------------------
void __fastcall TSendTesterReportForm::FormKeyDown(TObject *Sender, WORD &Key, TShiftState Shift)
{
	if(Key == VK_RETURN && Shift.Contains(ssCtrl)) {
		Key = NULL;
		if(ReportCategoryRadioGroup->ItemIndex >= 0) SendButton->Click();
		else ReportCategoryRadioGroup->Buttons[0]->SetFocus();
	}
	else if(Key == VK_ESCAPE &&
		(EditReportTextOnStart == ReportDetailsDBEditEh->Text ||
		 MessageDlg("Cancel changes and exit ?", mtConfirmation, TMsgDlgButtons()<<mbYes<<mbNo, NULL, mbNo) == mrYes))
	{
		Close();
	}

}
//---------------------------------------------------------------------------

